<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
	Statement query2 = dbconn.createStatement();
%>

<%
	String maLDT = request.getParameter("maldt");
	if (maLDT != null) {
		HttpSession ss = request.getSession();
		if (ss.getAttribute("maTS") != null) {
			String maTS = ss.getAttribute("maTS").toString();
			ResultSet rs = query.executeQuery("select maDT, count(cauhoi) as SoCH from dethi left join ct_dethi on maDT = dethi "
					+ " where loaidethi = " + maLDT + " group by maDT order by maDT asc");
			while (rs.next()) {
				out.println("<div class='item " + Integer.toString(rs.getInt("maDT"))
						+ "' onclick=\" dethi_select('" + Integer.toString(rs.getInt("maDT")) + "'); \" >");

				out.println("<div class='header'>Mã đề thi : <b>");
				out.println(Integer.toString(rs.getInt("maDT")));
				out.println("</b></div>");

				out.println("<div class='info'>Số lượng câu hỏi : <b>");
				out.println(Integer.toString(rs.getInt("SoCH")));
				out.println("</b></div>");
				
				ResultSet rs2 = query2.executeQuery("select count(*) as Soluong from baithi "
						+ " where dethi = "  + Integer.toString(rs.getInt("maDT")) + " and thisinh = " + maTS);
				rs2.next();
				if (rs2.getInt("Soluong") > 0) {
					out.println("<div class='info'><b>Đã từng thi</b></div>");
				} else {
					out.println("<div class='info'><i>Chưa thi</i></div>");
				}
				rs2.close();

				out.println("</div>");
			}
			rs.close();
		}
	}
%>

<% dbconn.close();%>